<template>
  <!-- 开票审核 invoice_order_listopen.jsp -->
  <div class="jeecg-basic-table jeecg-basic-table-form-container">
    <a-form @keyup.enter.native="searchQuery">
      <a-row :gutter="8" type="flex">
        <a-col flex="128px">
          <a-form-item label="状态" :label-col="{ style: { width: '40px' } }" >
            <JDictSelectTag
              v-model:value="queryParam.stat"
              mode="multiple"
              dictCode="invoiceDdzt"
              placeholder="全部"
              :maxTagCount="0"
              :dropdownMatchSelectWidth="false"
              :allowClear="true"
            />
          </a-form-item>
        </a-col>

        <a-col flex="145px">
          <a-form-item label="操作人" :label-col="{ style: { width: '55px' } }">
            <JDictSelectTag
              v-model:value="queryParam.userIds"
              mode="multiple"
              dictCode="sys_user,realname,id@authUser"
              placeholder="全部"
              :maxTagCount="0"
              :dropdownStyle="{ width: '120px', overflow: 'auto' }"
              :dropdownMatchSelectWidth="false"
              :allowClear="true"
            />
          </a-form-item>
        </a-col>

        <a-col flex="345px">
          <a-form-item label="" :label-col="{ style: { width: '70px' } }" :wrapper-col="{ style: { width: '250px' } }">
            <a-select v-model:value="queryParam.sdate" default-value="0" id="today" style="width: 90px; margin-right: 5px" @change="todayChange" >
              <a-select-option value="0">操作日期</a-select-option>
              <a-select-option value="1">出团日期</a-select-option>
            </a-select>
            <a-date-picker
              v-model:value="queryParam.beginDate"
              :disabled-date="disabledStartDate"
              valueFormat="YYYY-MM-DD"
              placeholder=""
              style="width: 109px; padding: 4px"
            /> ~
            <a-date-picker
              v-model:value="queryParam.endDate"
              :disabled-date="disabledEndDate"
              valueFormat="YYYY-MM-DD"
              placeholder=""
              style="width: 109px; padding: 4px"
            />
          </a-form-item>
        </a-col>

        <a-col flex="175px">
          <a-form-item label="登记编号" :label-col="{ style: { width: '65px' } }">
            <a-input placeholder="" v-model:value="queryParam.orderId" ></a-input>
          </a-form-item>
        </a-col>

        <a-col flex="165px">
          <a-form-item label="订单号" :label-col="{ style: { width: '50px' } }">
            <a-input placeholder="" v-model:value="queryParam.businessId" ></a-input>
          </a-form-item>
        </a-col>

        <a-col flex="180px">
          <a-form-item label="单位名称" :label-col="{ style: { width: '65px' } }">
            <a-input placeholder="" v-model:value="queryParam.dwmc"></a-input>
          </a-form-item>
        </a-col>

        <a-col flex="auto" style="float:right; width:90px; margin-right: 0px;">
          <span style="float: right;overflow: hidden;" class="table-page-search-submitButtons">
            <a-button type="primary" @click="searchQuery" preIcon="ant-design:search">查询</a-button>
          </span>
        </a-col>
      </a-row>
    </a-form>
  </div>

  <!--引用表格 :rowSelection="rowSelection"-->
  <BasicTable @register="registerTable"
              :onRowDbClick="handleDblclick"
              :onRowClick="handleClick"
              :rowClassName="rowClassName"
              :loading="loading"
  >
  </BasicTable>
  <!-- table区域-end v-auth="'business:delete'"-->

  <OpenAccountOrder @register="registerBusinessOrder" @success="reload"></OpenAccountOrder>

</template>

<script lang="ts" setup name="invoiceOrder-backOrderList">
import { ref, computed, unref, reactive, toRaw, watch } from "vue";
import { BasicColumn, BasicTable } from "/@/components/Table";
import { useModal } from "/@/components/Modal";
import { useListPage } from "/@/hooks/system/useListPage";
import { momentSearchQueryDate } from "/@/utils/dateUtil";
import { backOrderList } from "/@/views/full/invoice/types/invoice.api";
import { render } from "/@/utils/common/renderUtils";
import moment from 'dayjs'
import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
import OpenAccountOrder from './modules/openAccountOrder.vue'

//初始化 -------------------------------------------------------------
//定义查询表单，查询条件
const queryParam = reactive<any>({
  type: 'back',
  beginDate : momentSearchQueryDate('sub'),
  endDate: momentSearchQueryDate('add'),
});

//限制查询日期，开始日期不能大于结束日期
function disabledStartDate(beginDate) {
  const endDate = queryParam.endDate;
  if (!beginDate || !endDate) {
    return false;
  }
  return beginDate.valueOf() > moment(endDate).valueOf()
}

function disabledEndDate(endDate) {
  const beginDate = queryParam.beginDate;
  if (!endDate || !beginDate) {
    return false;
  }
  return moment(beginDate).valueOf() > endDate.valueOf();
}
//初始化 -------------------------------------------------------------

function searchQuery() {
  setProps({searchInfo:toRaw(queryParam)});
  reload()
}

function searchReset() {
  Object.assign(queryParam, {ddlxIds: '', ddzt: '', ywlx: ''});
}

function handleSummary(tableData: Recordable[]) {
  const res = getRawDataSource()
  redoHeight();
  if (res) {
    return getRawDataSource().summary;
  } else {
    return [];
  }
}

function changeWskchecked(e) {
  queryParam.wskchecked = e.target.checked;
}

//订单列表 - 状态转译
const statusFmt = ({text}: { text: any }) => {
  const color = text == '0' ? 'green' : text == '1' ? 'red' : 'gray';
  return render.renderTag(render.renderDict(text, 'invoiceDdzt'), color)
}

//订单列表 - 表头
const columns: BasicColumn[] = [
  { title: "状态", dataIndex: "stat", width: 55, align: "center", customRender: statusFmt },
  { title: "类型", dataIndex: "sflx", width: 43, align: "center", className:"text-green-500", customRender: ({ text }) => render.renderDict(text, "sflxType")},
  { title: "登记编号", dataIndex: "orderId", width: 105, align: "center" },
  { title: "单位名称", dataIndex: "dwmc", width: 240, align: "left" },
  { title: "已收款", dataIndex: "skje", align: "right", width: 80, className:"text-red-600" },
  { title: "已付款", dataIndex: "fkje", align: "right", width: 85, className:"text-green-600" },
  { title: "已回票", dataIndex: "fpfk", align: "right", width: 85, className:"text-red-600" },
  { title: "票差", dataIndex: "fpce", align: "right", width: 85, className:"text-blue-600" },
  { title: "申请金额", dataIndex: "sqje", align: "right", width: 85, className:"text-green-600" },
  { title: "交接人", dataIndex: "lxr", align: "center", width: 65 },
  { title: "操作人", dataIndex: "realname", align: "center", width: 65 },
  { title: "操作日期", dataIndex: "createDate", align: "center", width: 90 },
]

// 列表页面公共参数、方法
const loading     = ref<boolean>(false);
const dataSource  = ref<any[]>([]);
const summaryData = ref<any[]>([]);
const {prefixCls, tableContext, onExportXls, onImportXls} = useListPage({
  designScope: 'bank-order-list',
  tableProps: {
    rowKey: 'id',
    api: backOrderList,
    columns: columns,
    showIndexColumn: true,    //行号
    clickToRowSelect: true,   //点击选择
    showActionColumn: false,
    showTableSetting: false,
    useSearchForm: false,
    showSummary: false,
    beforeFetch: (params) => Object.assign(params, unref(queryParam)),
  },
})

//注册table数据
const [registerTable, {reload, setProps, updateTableDataRecord, getRawDataSource, redoHeight}] = tableContext

//新增订单
const [registerBusinessAdd, {openModal: openBusinessAdd}] = useModal();
/**
 * 新增事件
 */
function handleCreate() {
  openBusinessAdd(true, { isUpdate: false });
}

/**
 * order 订单新增后回调函数,自动弹出订单编辑窗口
 * @param businessId
 */
function handleSuccess(businessId) {
  let data = {id: businessId, type: queryParam.type}
  openBusinessOrder(true, data);
  reload();
}

//详细订单
const [registerBusinessOrder, {openModal: openBusinessOrder}] = useModal();
//双击行
function handleDblclick(record) {
  let data = {id: record.id, type: queryParam.type}
  openBusinessOrder(true, data);
}

//单击行
let selectRows = reactive<any>({id: '', username: ''});
function handleClick(record) {
  selectRows.id = record.id
  selectRows.username = record.username
}

function rowClassName(record, index) {
  return record.id === selectRows.id ? 'row-selected' : '';
}

/**
 * 新增事件
 */
async function handleDelete() {
  if (!selectRows || selectRows.length == 0) {
    alert('请选择数据。');
    return;
  }

  await businessDelete({id: selectRows.id}, reload)
}

/**
 * 收支项目 注册弹出编辑窗口
 * 作者：qdl
 * 日期：2022-07-04
 */
const [registerAccount, { openModal: accountModal }] = useModal();
//收支项目 - 添加
function handleAccountAdd() {
  accountModal(true, {id: 1});
}

function todayChange() {
  if (queryParam.today == '1') {
    queryParam.shbj = '1'
  } else {
    queryParam.shbj = '0'
  }
}
</script>
<style lang="less" scoped>

</style>
